/**
 * @author thaodo
 *
 *
 */
package com.mfsoft.camera;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;

import com.mfsoft.adapter.TabsPageAdapter;
import com.mfsoft.fragment.ImageServerFragment;
import com.mfsoft.utilities.Variables;

//min = 11 tro len
@SuppressLint("NewApi")
public class OpenImageTabsActivity extends FragmentActivity implements
		TabListener {

	private ViewPager viewPager;
	private TabsPageAdapter tAdapter;
	private ActionBar actionBar;
	// tab titles
	private String[] tabs = { "Album cua toi", "Anh tu server" };

	// private NetworkChangeReceiver receiver;

	// private boolean isConnected = false;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.tab_layout);

		// IntentFilter filter = new IntentFilter(
		// ConnectivityManager.CONNECTIVITY_ACTION);
		// receiver = new NetworkChangeReceiver();
		// registerReceiver(receiver, filter);

		init();
		int frag = getIntent().getExtras().getInt(Variables.FRAGMENT);
		// System.out.println("tab: " + frag);
		viewPager.setCurrentItem(frag);
		actionBar.setSelectedNavigationItem(frag);
		// tAdapter.getItem(frag);

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// chon tab nao, action bar se hien thi nhan vao tab do
				actionBar.setSelectedNavigationItem(position);
				// if (position == 1) {
				// tAdapter.getImageServerFragment().openNetworkSetting();
				// }
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void init() {
		viewPager = (ViewPager) findViewById(R.id.tpOpenImage);
		actionBar = getActionBar();
		tAdapter = new TabsPageAdapter(getSupportFragmentManager());

		viewPager.setAdapter(tAdapter);
		actionBar.setHomeButtonEnabled(false);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		// int frag = getIntent().getExtras().getInt(Variables.FRAGMENT);
		// tAdapter.getItem(frag);
		// viewPager.setCurrentItem(frag);

		// add tabs
		for (String tab_name : tabs) {
			actionBar.addTab(actionBar.newTab().setText(tab_name)
					.setTabListener(this));
		}

//		tAdapter.getImageServerFragment().registerReceiver();
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// chon tab nao hien thi giao dien tab do
		// if (tab.getPosition() == 1) {
		// tAdapter.getImageServerFragment().openNetworkSetting();
		// }
		// Variables.FRAG = tab.getPosition();
		// if (tab.getPosition() == 1) {
		// tAdapter.getImageServerFragment().openNetworkSetting();
		// }
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		viewPager.setCurrentItem(tab.getPosition());
		// Variables.FRAG = tab.getPosition();
		System.out.println("tab: " + tab.getPosition());
		if (tab.getPosition() == 1) {
			//tAdapter.getImageServerFragment().openNetworkSetting();
		}
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.v(Variables.LOG_NETWORK, "onResume");
//		tAdapter.getImageServerFragment().registerReceiver();
		if (viewPager.getCurrentItem() == 1) {
			tAdapter.getImageServerFragment().openNetworkSetting();
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.v(Variables.LOG_NETWORK, "onPause");
		// unregisterReceiver(receiver);
//		tAdapter.getImageServerFragment().unregisterReceiver();;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.v(Variables.LOG_NETWORK, "onDestroy");
		// unregisterReceiver(receiver);
//		tAdapter.getImageServerFragment().unregisterReceiver();
	}

	// class NetworkChangeReceiver extends BroadcastReceiver {
	//
	// // listen network change
	// @Override
	// public void onReceive(Context context, Intent intent) {
	// Log.v(Variables.LOG_NETWORK,
	// "Received notification about network status");
	// isNetworkAvailabel(context);
	//
	// }
	//
	// private boolean isNetworkAvailabel(Context context) {
	// ConnectivityManager connectivity = (ConnectivityManager) context
	// .getSystemService(Context.CONNECTIVITY_SERVICE);
	// if (connectivity != null) {
	// NetworkInfo[] info = connectivity.getAllNetworkInfo();
	// if (info != null) {
	// for (int i = 0; i < info.length; i++) {
	// if (info[i].getState() == NetworkInfo.State.CONNECTED) {
	// // if (!ImageServerFragment.isConnected) {
	// // Log.v(Variables.LOG_NETWORK,
	// // "Now you are connected Internet");
	// // ImageServerFragment.isConnected = true;
	// // // muon lam gi thi lam o day
	// // // post data hoac update tu server ...
	// // if (viewPager.getCurrentItem() == 1) {
	// // tAdapter.getImageServerFragment()
	// // .openNetworkSetting();
	// // }
	// // }
	// return true;
	// }
	//
	// }
	// }
	// }
	// Log.v(Variables.LOG_NETWORK, "you are not connected Internet");
	// // ImageServerFragment.isConnected = false;
	// return false;
	// }
	//
	// }

}
